<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\View;
Class Zhuanji extends Index
{
	public function __construct(Request $request)
	{
		parent::__construct($request);
	}
	/**
	* 发表传记
	*/
	public function zhuanjiAdd()
	{
		if(!session('userid')){
			jsBack('您还未登录,前去登陆','/api/register/index');
			return false;
		}else{
			return view('zhuanjiadd');
		}
	}
	/*
	* 添加操作
	*/
	public function zhuanjiInsert()
	{
		if(request()->isPost()){
			if(input('post.zhuanjititle')=="" || input('post.zhuanjicontent')==''){
				jsBack('标题和内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('zhuanjiphotp');
			if($file){
			    // 移动到框架应用根目录/public/uploads/zhuanji 目录下
		        $info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/zhuanji');
		        //上传成功后获取传记信息
		        if($info){
		        	$data['zhuanjiphoto']='/public/uploads/zhuanji/'.date("Ymd").'/'.$info->getFilename();
			        $data['zhuanjititle']=input('post.zhuanjititle');
					$data['zhuanjicontent']=input('post.zhuanjicontent');
					$data['addtime']=time();
					$data['userid']=session('userid');
					$zhuanji=Db::name('zhuanji')->insert($data);
					//传记添加
					if($zhuanji){
						jsBack('添加成功','/api/zhuanji/zhuanjilist');
					}else{
						jsBack('网络有误,请重试');
					}
		        }else{//输出错误信息
		        	jsBack($file->getError());
		            return false;
		        }
		    }else{
		    	jsBack('请选择传记展示图');
		    }
		}else{
			//如果不是post跳转会添加页
			$this->redirect('zhuanji/zhuanjiadd');
		}
	}
	/**
	* 传记列表
	*/
	public function zhuanjiList()
	{
		$page=Db::name('zhuanji')->paginate(6,false,['type' => 'bootstrap','var_page' => 'page']);
		$zhuanji=$page->all();
		foreach ($zhuanji as $key => $value) {
		$zhuanji[$key]['nickname']=Db::name('user')->where(['userid'=>$zhuanji[$key]['userid']])->value('nickname');
		}
		$this->assign('zhuanji',$zhuanji);
		$this->assign('page',$page);
		$this->assign('title','读传记');
		return view('zhuanjilist');
	}
	/**
	* 传记详情
	*/
	public function zhuanjiDetail()
	{
		//传记
		$zhuanjidetail=Db::name('zhuanji')->where(['zhuanjiid'=>input('zhuanjiid')])->find();
		$zhuanjidetail['nickname']=Db::name('user')->where(['userid'=>$zhuanjidetail['userid']])->value('nickname');
		//评论
		$comment=Db::name('comment')->where(['zhuanjiid'=>$zhuanjidetail['zhuanjiid']])->select();
		foreach ($comment as $key => $value) {
			$comment[$key]['nickname']=Db::name('user')->where(['userid'=>$comment[$key]['userid']])->value('nickname');
			$comment[$key]['photo'] = Db::name('user')->where(['userid'=>$comment[$key]['userid']])->value('photo');
			if($comment[$key]['huifuid']!=0){
				$comment[$key]['huifunickname'] = Db::name('user')->where(['userid'=>$comment[$key]['huifuuserid']])->value('nickname');
			}else{
				$comment[$key]['huifunickname']='';
			}
		}
		if(empty($comment)){
			$comment='';
		}
		$this->assign('title',$zhuanjidetail['zhuanjititle']);
		$this->assign('comment',$comment);
		$this->assign('zhuanji',$zhuanjidetail);
		return view('zhuanjidetail');
	}
	/**
	* 传记评论
	*/
	public function zhuanjiComment()
	{
		$data['zhuanjiid']=input('post.zhuanjiid');
		$data['comment']=htmlspecialchars(input('post.comment'));
		$data['userid']=session('userid');
		$data['commenttime']=time();
		$data['huifuid']=input('post.huifuid');
		$data['huifuuserid']=input('post.huifuuserid');
		$comment=Db::name('comment')->insert($data);
		if($comment){
			jsBack('评论成功','/api/zhuanji/zhuanjidetail?zhuanjiid='.input('post.zhuanjiid'));
		}else{
			jsBack('评论失败');
		}
	}
	/**
	* 相册
	*/ 
	public function photoList()
	{
		if(!session('userid')){
			jsBack('您还未登录,前去登陆','/api/register/index');
			return false;
		}else{
			$page=Db::name('photos')->order('phototime desc')->where(['userid'=>session('userid')])->paginate(6,false,['type' => 'bootstrap','var_page' => 'page']);
			$photoslist=$page->all();
			foreach ($photoslist as $key => $value) {
				$photoslist[$key]['photosde']=Db::name('photode')->where(['photosid'=>$photoslist[$key]['photosid']])->select();
			}
			$this->assign('page',$page);
			$this->assign('list',$photoslist);
			return view('photolist');
		}
		
	}
	/**
	* 相册上传
	*/
	public function photoAdd()
	{
		return view('photoadd');
	}
	public function photoinsert()
	{
		$file = request()->file('file');
		if($file){
		    // 移动到框架应用根目录/public/uploads/zhuanji 目录下
	        $info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/xiangce');
	        //上传成功后获取传记信息
	        if($info){
	        	$data['photosdir']='/public/uploads/xiangce/'.date("Ymd").'/'.$info->getFilename();
		        $data['photocontent']=input('post.photocontent');
				$data['photostitle']=input('post.photostitle');
				$data['phototime']=time();
				$data['userid']=session('userid');
				$photos=Db::name('photos')->insertGetId($data);
				//添加相册
				if($photos){
					$files = request()->file('files');
					foreach ($files as $value) {
						$info = $value->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/xiangce');
						$data_p['photosid']=$photos;
						$data_p['photodedir']='/public/uploads/xiangce/'.date("Ymd").'/'.$info->getFilename();
						$photode=Db::name('photode')->insert($data_p);
					}
					if($photode){
						jsBack('创建成功','/api/zhuanji/photolist');
					}else{
						Db::name('photos')->where(['photosid'=>$photos])->delete();
						jsBack('创建失败');
					}
				}else{
					jsBack('创建失败,请重试');
				}
			}
		}else{
			jsBack('请选择相册展示图');
			return false;
		}
	}
	/**
	* 相册详情
	*/
	public function photodetails()
	{
		if(!session('userid')){
			jsBack('您还未登录,前去登陆','/api/register/index');
			return false;
		}else{
			$where['photosid']=input('photosid');
			$this->photodetail($where);
			return view('photodetails');
		}
	}
	/**
	* 相册修改
	*/
	public function photosedit()
	{
		if(!session('userid')){
			jsBack('您还未登录,前去登陆','/api/register/index');
			return false;
		}else{
			$where['photosid']=input('photosid');
			$this->photodetail($where);
			return view('photosedit');
		}
	}
	/**
	* 相册修改
	*/
	public function photosUpdate()
	{
		$where['photosid']=input('photosid');
		$file = request()->file('file');
		$files = request()->file('files');
		$photos=0;
		$photode=0;
		//跟换展示图
		if($file){
		    // 移动到框架应用根目录/public/uploads/zhuanji 目录下
	        $info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/xiangce');
	        $photosdir=Db::name('photos')->where($where)->value('photosdir');
	        //上传成功后获取传记信息
	        if($info){
	        	$data['photosdir']='/public/uploads/xiangce/'.date("Ymd").'/'.$info->getFilename();
		        $data['photocontent']=input('post.photocontent');
				$data['photostitle']=input('post.photostitle');
				$data['phototime']=time();
				$data['userid']=session('userid');
				@unlink('.'.$photosdir);
				$photos=Db::name('photos')->where($where)->update($data);
			}
		}elseif($files){
		//只新添加了图片 
			foreach ($files as $value){
				$info = $value->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/xiangce');
				$data_p['photosid']=input('photosid');
				$data_p['photodedir']='/public/uploads/xiangce/'.date("Ymd").'/'.$info->getFilename();
				$photode=Db::name('photode')->insert($data_p);
			}
		}else{
			//只修改标题或者内容但没有修改任何图片
			$data['photocontent']=input('post.photocontent');
			$data['photostitle']=input('post.photostitle');
			$data['phototime']=time();
			$data['userid']=session('userid');
			$photos=Db::name('photos')->where($where)->update($data);
		}
		//修改成功
		if($photos || $photode){
			jsBack('修改成功','/api/zhuanji/photoList');
		}else{
			jsBack('修改失败');
		}
	}
	/**
	* 删除相册/相片
	* 
	*/
	public function photosdelete()
	{
		if(!request()->isAjax()){
			jsBack('请求有误');
			return false;
		}
		$where['photodeid']=input('post.photodeid');
		$photode=Db::name('photode')->where($where)->find();
		@unlink('.'.$photode['photodedir']);
		$photodedel=Db::name('photode')->where($where)->delete();
		if($photodedel){
			res('success','删除成功');
		}else{
			res('error','删除失败');
		}
	}
	/**
	* 单条记录
	*/
	private function photodetail($where)
	{
		
		$photos=Db::name('photos')->where($where)->find();
		$photos['photode']=Db::name('photode')->where($where)->select();
		$list=Db::name('photos')->order('phototime desc')->limit(4)->select();
		$this->assign('photos',$photos);
		$this->assign('list',$list);
		$this->assign('title',$photos['photostitle']);
	} 
}